package org.ybxb.demo.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spi.service.contexts.SecurityContextBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2  // 开启Swagger支持
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 将api的元信息设置为包含在json resourcelisting响应中
                .apiInfo(apiInfo()).enable(true) // 是否启用Swagger，如果为false，则swagger不能在浏览器中访问
                // 设置ip和端口或域名
                // .host("127.0.0.1:9000")
                .select()
                // .apis(RequestHandlerSelectors.any())                                            // 1
                // .匹配任何controller的接口
                // .apis(RequestHandlerSelectors.basePackage("org.ybxb.demo.user.controller"))     // 2.扫描指定包路径
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))                      // 3.扫描含有类注解的
                // .apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class))       // 4.扫描含有方法注解的
                .paths(PathSelectors.any())                                                        //
                // .paths(PathSelectors.ant("/api/*"))                                             //
                .build().securityContexts(securityContexts()).securitySchemes(securitySchemes());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("Apaas平台", "https://www.ctapaas.cn/", "baihy09@chinatelecom.cn");
        return new ApiInfoBuilder().contact(contact)    // 联系人
                .title("Apaas组件")                      // 文档标题
                .description("Apaas组件Restful Api文档")  // 文档描述
                .version("1.0.0")                        // 定义版本号
                .build();
    }

    /**
     * 设置需要的登录认证的路径s
     */
    private List<SecurityContext> securityContexts() {
        SecurityContextBuilder builder = SecurityContext.builder();
        builder.securityReferences(defaultAuth());
        builder.forPaths(PathSelectors.ant("/api/*"));
        return Collections.singletonList(builder.build());
    }

    /**
     * 设置请求头的信息
     */
    private List<SecurityScheme> securitySchemes() {
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "Header");
        return Collections.singletonList(apiKey);
    }

    /**
     * 定义授权范围
     */
    private List<SecurityReference> defaultAuth() {
        List<AuthorizationScope> scopeList = Collections.singletonList(new AuthorizationScope("global", "全局认证"));
        return Collections.singletonList(new SecurityReference("Authorization",
                scopeList.toArray(new AuthorizationScope[0])));
    }
}